﻿@using Flagrum.Web.Persistence.Entities
@using Flagrum.Web.Features.WorkshopMods.Data
@inject FlagrumDbContext Context
@inject NavigationManager Navigation
@inject IStringLocalizer<ModelReplacementRow> Localizer

<div class="box mt-2 p-3 row hover:bg-dark-550 cursor-pointer" @onclick="OnSelect">
    <span class="material-icons cursor-pointer @(Preset.IsFavourite ? "text-accent1-200" : "")"
          @onclick="OnFavourite"
          @onclick:stopPropagation="true">
        @(Preset.IsFavourite ? "star" : "star_border")
    </span>
    <span class="flex-grow mx-4">@Preset.Name</span>
    <span class="material-icons-outlined cursor-pointer mr-3"
          @onclick="() => InformationModal.Open()"
          @onclick:stopPropagation="true">
        info
    </span>
    <span class="material-icons cursor-pointer"
          @onclick="CreateCopy"
          @onclick:stopPropagation="true">
        content_copy
    </span>
    @if (!Preset.IsDefault)
    {
        <span class="material-icons cursor-pointer ml-3"
              @onclick="Edit"
              @onclick:stopPropagation="true">
            edit
        </span>
        <span class="material-icons cursor-pointer ml-3"
              @onclick="ConfirmDelete"
              @onclick:stopPropagation="true">
            delete
        </span>
    }
</div>

<AutosizeModal @ref="InformationModal" Width="800" Height="480">
    <HeaderView>
        <span class="text-grey-300 flex-grow">@Localizer["PresetInformation"]</span>
        <span class="material-icons cursor-pointer" @onclick="() => InformationModal.Close()">cancel</span>
    </HeaderView>
    <BodyView>
        <div style="max-height: 80vh">
            <strong>@Localizer["PresetReplace"]</strong>
            <ul class="list-disc ml-6 mt-2">
                @foreach (var path in Preset.Paths)
                {
                    <li>@path</li>
                }
            </ul>
        </div>
    </BodyView>
</AutosizeModal>

<PromptModal Title="Delete Preset"
             @ref="DeleteModal"
             Heading="Are you sure?"
             Subtext="This action cannot be undone!"
             YesText="Delete"
             NoText="Cancel"
             OnYes="Delete"/>

@code
{
    [CascadingParameter]
    public ModelReplacements Parent { get; set; }

    [CascadingParameter]
    public Index ParentParent { get; set; }

    [Parameter]
    public ModelReplacementPresetMetadata Preset { get; set; }

    private AutosizeModal InformationModal { get; set; }
    private PromptModal DeleteModal { get; set; }

    private void OnSelect()
    {
        var id = Preset.IsDefault ? Preset.Id : Preset.Id + 100;
        ParentParent.Mod.Target = id;
        ParentParent.ModelReplacementPresetName = Preset.Name;
        ParentParent.ModTargetChanged(id);
    }

    private void OnFavourite()
    {
        if (Preset.IsFavourite)
        {
            var match = Context.ModelReplacementFavourites
                .FirstOrDefault(f => f.Id == Preset.Id && f.IsDefault == Preset.IsDefault);

            if (match != null)
            {
                Context.Remove(match);
            }

            Preset.IsFavourite = false;
        }
        else
        {
            Context.ModelReplacementFavourites.Add(new ModelReplacementFavourite
            {
                Id = Preset.Id,
                IsDefault = Preset.IsDefault
            });

            Preset.IsFavourite = true;
        }

        Context.SaveChanges();
        Parent.CallStateHasChanged();
    }

    private void CreateCopy()
    {
        Navigation.NavigateTo($"/mod/presets/copy/{Preset.Id}/{Preset.IsDefault}");
    }

    private void Edit()
    {
        Navigation.NavigateTo($"/mod/presets/edit/{Preset.Id}/false");
    }

    private void ConfirmDelete()
    {
        DeleteModal.Open();
    }

    private void Delete()
    {
        var match = Context.ModelReplacementPresets.Find(Preset.Id);

        if (match != null)
        {
            Context.Remove(match);
            Context.SaveChanges();
        }

        Parent.RemovePreset(Preset.Id);
    }
}